﻿//
//  HFHttpLJCFactory.m
//  HFBaseLibModule
//
//  Created by Points on 2020/4/3.
//  Copyright © 2020 hfqf123@126.com. All rights reserved.
//

#import "HFHttpLJCFactory.h"

@implementation HFHttpLJCFactory

- (id<HFHttpAbstractFactory> (^)(void))post{
    __block __weak typeof(self) weakSelf = self;
    return ^(void){
        if (self.rsaEncryptResult) {
            BOOL needToast = weakSelf.needToast;
            BOOL rsaSwitch = weakSelf.rsaSwitch;
            HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
            HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
            NSDictionary *para = weakSelf.mPara;
            NSURLSessionDataTask *task = [HFBaseHTTPRequestManager LJCPOST:weakSelf.mPath parameters:weakSelf.mPara success:^(NSURLSessionDataTask *task, id responseObject) {
                [HFHttpLJCFactory requestSuccessWithTask:task
                                                     body:para
                                           responseObject:responseObject
                                           successHandler:successBlock
                                                needToast:needToast
                                                rsaSwitch:rsaSwitch];
            } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                [HFHttpLJCFactory requestFailureWithTask:task
                                                    param:weakSelf.mPara
                                                    error:error
                                           failureHandler:failBlock
                                                needToast:needToast];
            }];
            weakSelf.task = task;
            [weakSelf startDelayTimer:task];
        }
       
        return weakSelf;
    };
}

- (id<HFHttpAbstractFactory> (^)(void))get{
     __block __weak typeof(self) weakSelf = self;
     return ^(void){
         BOOL needToast = weakSelf.needToast;
         BOOL rsaSwitch = weakSelf.rsaSwitch;
         HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
         HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
         NSDictionary *para = weakSelf.mPara;
         NSURLSessionDataTask *task = [HFBaseHTTPRequestManager LJCGET:weakSelf.mPath parameters:weakSelf.mPara success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
             [HFHttpLJCFactory requestSuccessWithTask:task
                                                  body:para
                                        responseObject:responseObject
                                        successHandler:successBlock
                                             needToast:needToast
                                             rsaSwitch:rsaSwitch];
         } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
             [HFHttpLJCFactory requestFailureWithTask:task
                                                 param:weakSelf.mPara
                                                 error:error
                                        failureHandler:failBlock
                                             needToast:needToast];
         }];
         weakSelf.task = task;
         return weakSelf;
     };
}

- (id<HFHttpAbstractFactory> (^)(void))singleUpload {
    __block __weak typeof(self) weakSelf = self;
    return ^(void) {
        BOOL needToast = weakSelf.needToast;
        BOOL rsaSwitch = weakSelf.rsaSwitch;
        HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
        HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
        NSDictionary *para = weakSelf.mPara;
        NSURLSessionDataTask *task = [HFBaseHTTPRequestManager LJCPOST:weakSelf.mPath parameters:weakSelf.mPara constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            //保持来聚财原有逻辑，增加类型判断，
            NSString *uuidString = [NSString string];
            if(weakSelf.mFileName){
                //将得到的二进制图片拼接到表单中 /** data,指定上传的二进制流;name,服务器端所需参数名*/
                [formData appendPartWithFileData:weakSelf.mImage
                                            name:weakSelf.mImageName?:@"image"
                                        fileName:weakSelf.mFileName
                                        mimeType:weakSelf.mMimeType?:@"image/png"];
            }else{
                if ([weakSelf.mMimeType isEqualToString:@"audio/mpeg"]) {
                    uuidString = [NSUUID.UUID.UUIDString stringByAppendingString:@".mp3"];
                } else if ([weakSelf.mMimeType isEqualToString:@"image/png"]) {
                    uuidString = [NSUUID.UUID.UUIDString stringByAppendingString:@".png"];
                } else if ([weakSelf.mMimeType isEqualToString:@"video/mp4"]) {
                    uuidString = [NSUUID.UUID.UUIDString stringByAppendingString:@".mp4"];
                } else {
                    //默认png格式
                    uuidString = [NSUUID.UUID.UUIDString stringByAppendingString:@".png"];
                }
                //将得到的二进制图片拼接到表单中 /** data,指定上传的二进制流;name,服务器端所需参数名*/
                [formData appendPartWithFileData:weakSelf.mImage name:weakSelf.mImageName?:@"image" fileName:uuidString mimeType:weakSelf.mMimeType?:@"image/png"];
            }
         
   
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            [HFHttpLJCFactory requestSuccessWithTask:task
                                                 body:para
                                       responseObject:responseObject
                                       successHandler:successBlock
                                            needToast:needToast
                                            rsaSwitch:rsaSwitch];
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            [HFHttpLJCFactory requestFailureWithTask:task
                                                param:weakSelf.mPara
                                                error:error
                                       failureHandler:failBlock
                                            needToast:needToast];
        }];
        weakSelf.task = task;
        return weakSelf;
    };
}

- (id<HFHttpAbstractFactory> (^)(void))multiUpload {
    __block __weak typeof(self) weakSelf = self;
    return ^(void) {
        BOOL needToast = weakSelf.needToast;
        BOOL rsaSwitch = weakSelf.rsaSwitch;
        HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
        HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
        NSDictionary *para = weakSelf.mPara;
        NSURLSessionDataTask *task = [HFBaseHTTPRequestManager LJCPOST:weakSelf.mPath parameters:weakSelf.mPara constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            for (int i = 0; i < weakSelf.mImageArray.count; i++) {
                NSString *uuidString = [NSUUID.UUID.UUIDString stringByAppendingString:@".png"];
                //将得到的二进制图片拼接到表单中 /** data,指定上传的二进制流;name,服务器端所需参数名*/
                [formData appendPartWithFileData:weakSelf.mImage name:[NSString stringWithFormat:@"image%d",i] fileName:uuidString mimeType:@"image/png"];
            }
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            [HFHttpLJCFactory requestSuccessWithTask:task
                                                 body:para
                                       responseObject:responseObject
                                       successHandler:successBlock
                                            needToast:needToast
                                            rsaSwitch:rsaSwitch];
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            [HFHttpLJCFactory requestFailureWithTask:task
                                                param:weakSelf.mPara
                                                error:error
                                       failureHandler:failBlock
                                            needToast:needToast];
        }];
        weakSelf.task = task;
        return weakSelf;
    };
}

@end

